<template>
  <div class="selectComponent" v-if="item">
    <span class="left">{{ item.title }}</span>
    <div class="right">
      <el-select v-model="value" placeholder="请选择" @change="selectOnChange">
        <el-option
          v-for="item in item.options"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
export default {
  name: "selectComponent",
  data() {
    return {
      value: null,
      //为了防止页面初始化时，触发不必要的通知更新，此处设置标识符，初始化后2秒置为true
      needUpdate: false,
    };
  },
  created() {},
  props: ["item", "rightIndex", "catalogIndex", "ocIndex", "id"],
  components: {},
  mounted() {
    this.$nextTick(() => {
      this.init();
    });
    setTimeout(() => {
      this.needUpdate = true;
    }, 1000);
  },
  methods: {
    init() {
      if (this.item) {
        this.value = null;
        this.$nextTick(() => {
          let oItem = JSON.parse(JSON.stringify(this.item));
          this.value = oItem.value || oItem.default;
        });
      }
    },
    selectOnChange() {
      if (this.needUpdate) {
        //标识是否不需要更新Main中的组件
        let needUpdateMainChild = true;
        // if (this.item.ename === "pageName") {
        //     needUpdateMain = false;
        // }
        //更新给直属父级
        this.$emit(
          "updateParent",
          JSON.stringify({
            value: this.value,
            ename: this.item.ename,
            rightIndex: this.rightIndex,
            catalogIndex: this.catalogIndex,
            needUpdateMainChild: needUpdateMainChild,
            needUpdateMain: false,
          })
        );
      }
    },
  },
  computed: mapState(["decorate"]),
  watch: {},
};
</script>
<style scoped lang="scss">
.selectComponent {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 0px;
  box-sizing: border-box;
  .left {
    width: 100px;
    display: flex;
    justify-content: flex-end;
    padding-right: 10px;
    box-sizing: border-box;
    font-size: 12px;
  }
  .right {
    display: flex;
    align-items: center;
    width: calc(100% - 100px);
  }
}
</style>